package report.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import report.dao.repository.ReportRepository;
import report.dao.util.ReturnMsgUtil;
import report.dao.vo.report.ReportVO;
import report.service.report.IReportService;

@Controller
public class ReportController {

    @Autowired
    private ReportRepository reportRepository;
    @Autowired
    private IReportService reportService;

    // 获取report
    @RequestMapping(value = "/report/all")
    @ResponseBody
    public ReturnMsgUtil queryReportAll() {
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( reportRepository.findAll() );
        res.setStatus("success");
        return res;
    }

    // 根据目录id获取report
    @RequestMapping(value = "/report/catalog/{catalogId}")
    @ResponseBody
    public ReturnMsgUtil queryReportByCataId(@PathVariable String catalogId) {
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( reportRepository.findAllByPid(catalogId) );
        res.setStatus("success");
        return res;
    }

    // 获取报表信息
    @RequestMapping(value = "/report/{reportId}")
    @ResponseBody
    public ReturnMsgUtil queryReportById(@PathVariable String reportId) {
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( reportRepository.findOne(reportId) );
        res.setStatus("success");
        return res;
    }

    // 添加report
    @PostMapping(value = "/report/create/{pid}")
    @ResponseBody
    public ReturnMsgUtil createReport(@PathVariable String pid, @RequestBody ReportVO form){
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( reportService.createReport(pid,form) );
        res.setStatus("success");
        return res;
    }


    // 更新report
    @PutMapping(value = "/report/update/{id}")
    @ResponseBody
    public ReturnMsgUtil updateReport(@PathVariable String id, @RequestBody ReportVO form){
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( reportService.updateReport(id, form));
        res.setStatus("success");
        return res;
    }

    // 删除report
    @DeleteMapping(value = "/report/delete/{id}")
    @ResponseBody
    public ReturnMsgUtil deleteReport(@PathVariable String id){
        ReturnMsgUtil res = new ReturnMsgUtil();
        res.setContent( reportService.deleteReport(id));
        res.setStatus("success");
        return res;
    }
}
